package com.glory.hikvisionmobile.activity;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.App;
import com.glory.hikvisionmobile.bean.ProcessSheet.Flow;
import com.glory.hikvisionmobile.bean.ProcessSheet.ProcessSheetBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.MyUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_LoadingDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_TabView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author yi.zhang
 * @date 2019/11/20
 * @desc 流程单查询
 */
public class Lot_ProcessSheetActivity extends FreeUI_BaseActivity {

    @BindView(R.id.useMateriel_Lv)
    ExpandableListView useMaterielLv;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tab_Lay)
    LinearLayout tabLay;
    @BindView(R.id.tvCurrentStep)
    TextView tvCurrentStep;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvPartDesc)
    TextView tvPartDesc;
    private List<Flow> flow;

    private FreeUI_LoadingDialog dialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_processsheet;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        String lot = bundle.getString("lot");
        if (!FreeApi_StringUtils.isEmpty(lot)){
            etLot.setText(lot);
            queryData(lot);
        }
        etLot.requestFocus();
    }

    @Override
    protected void initData() {
        FreeApi_EditTextUtils.setEditTextListener(etLot, this, s -> queryData(s));
        MyUtils.editActionListener(etLot, s -> queryData(s));
        EditTextEnterUtils.initEnterToUpperCase(etLot);
    }


    private void queryData(String lot) {
        dialog = new FreeUI_LoadingDialog();
        if (this.dialog != null) {
            this.dialog.show(getSupportFragmentManager(), "loading");
        }
        JsonObject map = new JsonObject();
        map.addProperty("lotId", lot);
        NetUtils.requestDara("HIK.PdaRunCard", this, map, new FreeUI_EntityCallBack<BaseResponseBean<ProcessSheetBean>>(
                new TypeToken<BaseResponseBean<ProcessSheetBean>>() {
                }.getType()) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ProcessSheetBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    ProcessSheetBean processSheetBean = response.body().getBody();
                    tvCurrentStep.setText(processSheetBean.getCurStepDesc());
                    tvPartDesc.setText(processSheetBean.getPartDesc());
                    tvPartName.setText(processSheetBean.getPartName());
                    flow = processSheetBean.getFlows();
                    fillData();

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ProcessSheetBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.PdaRunCard");
                if (dialog != null && !dialog.isHidden()) {
                    dialog.dismissAllowingStateLoss();
                }
            }
        });


    }

    private void fillData() {

        ArrayList<ArrayList<String>> dataList = new ArrayList<>();
        for (int i = 0; i < flow.size() + 1; i++) {
            ArrayList<String> columnList = new ArrayList<>();
            if (i == 0) {
                columnList.add("NO.");
                columnList.add("未来流程");
                columnList.add("未来工序描述");
                columnList.add("未来动作");
                columnList.add("未来动作备注");
            } else {
                columnList.add(i + "");
                columnList.add(flow.get(i - 1).getProcedureName());
                columnList.add(flow.get(i - 1).getFeatureStepDesc());
                columnList.add(flow.get(i - 1).getFeatureAction());
                columnList.add(flow.get(i - 1).getFeatureActionComment());
            }
            dataList.add(columnList);
        }
        FreeUI_TabView.getInstance().initTabView(App.app, tabLay, dataList)
                .defaultAttribute().setColumnWidth(0,8)
                .setCellPadding(10).setNullableString("").setTextViewSize(14)
                .setMinRowHeight(48).setMaxRowHeight(1000).show();
        FreeUI_TabView.getInstance().isRefreshOrLoading(false);
        if (this.dialog != null && !this.dialog.isHidden()) {
            this.dialog.dismissAllowingStateLoss();
        }


    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("流程单查询");
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        View v = getCurrentFocus();
        if (v != null && v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
        return super.dispatchTouchEvent(event);
    }

}
